// firstline.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "firstline.h"
#include <math.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_FIRSTLINE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FIRSTLINE));
			
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FIRSTLINE));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_FIRSTLINE);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//



#define N 100
typedef int mat[N][N];

#define minN 7
#define maxN 10

#define MaxV (3*n)

int ex,gr=0;

namespace prima
{
	int Procent = 30;
	int p[N];
	int d[N];

	int x;
	mat a;
	int n=0;
}

using namespace prima;

void resetprim()
{
    memset(p,-1,sizeof(d));
	memset(d,-1,sizeof(d));
	
	x = rand()%n;
	p[x] = x;
	d[x] = 1;     
}


void genrand() //genereaza valori pentru graf
{
	int i,j,x,m,k;
	int v[N];
	srand(time(0));
	memset(v,0,sizeof(v));
	memset(a,0,4*N*N);
	n = rand()%(maxN-minN+1)+minN;
	x =  rand()%n;
	v[x] = 1;
	m = 1;
	for(i=1;i<n;i++)
	{
		x = rand()%m;
		for(j=0;;j++)
			if(v[j]) if(x-- <= 0) break;

		x = rand()%(n-m);
		for(k=0;;k++)
			if(!v[k]) if(x-- <= 0) break;
		a[j][k] = a[k][j] = rand()%MaxV+1;
		m++;
		v[k] = 1;
	}
	
	for(i=0;i<n;i++)
	for(j=i+1;j<n;j++)
	if(rand()%100<Procent)
	a[i][j] = a[j][i] = rand()%MaxV+1;
}
int prim()    //alg propriu-zis
{
    int j;
		for(x=0;x<n;x++)
		if(d[x] > 0 )
		break;
		
		if(x==n) return 0;
		
		for(j=x+1;j<n;j++)
		if(d[j] > 0 && d[j] < d[x] )
		x = j;
		
		for(j=0;j<n;j++)
		if(a[x][j] > 0)
		{
			if(d[j]==-1 || (d[j] > 0 && d[j] > a[x][j]))
			{d[j]=a[x][j];p[j]=x;}
		}
		d[x] = 0;
		
	return 1;
}

void mark(int i, int &x, int &y)
{
	int c = 200;
	int r = 130;
	x = c + (int)floor(r * sin((double)i*710/113/n));
	y = c + (int)floor(r * cos((double)i*710/113/n));
}
void markm(int i, int &x, int &y)
{
	mark(i,x,y);
	x+=320;
}
void drawgraph(HDC hdc,HWND hWnd)
{
	int x,y,i,xx,yy;
	wchar_t buf[100];
	for(i=0;i<n;i++)
	{
		if( p[i] != i && p[i]!=-1)
		{
			mark(i,x,y);
			xx = x;
			yy = y;
			MoveToEx(hdc,x,y,0);
			mark(p[i],x,y);
			xx += x;
			yy += y;
			xx /=2;
			yy /=2;
			LineTo(hdc,x,y);
			swprintf(buf,L"%2d",a[i][p[i]]);
			TextOutW(hdc,xx,yy,buf,2);
		}
	}
	
	for(i=0;i<n;i++)
	{
		mark(i,x,y);
		if(d[i] == 0)
			swprintf(buf,L"O%2d",i);
		else
			swprintf(buf,L"%3d",i);
			TextOutW(hdc,x,y,buf,3);
	}
}

void drawmatrix(HDC hdc,HWND hWnd)
{
	int x,y;
	int xx,yy;
	int i,j;
	wchar_t buf[100];
	for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
		if(a[i][j] > 0)
		{
			markm(i,x,y);
			xx = x;
			yy = y;
			MoveToEx(hdc,x,y,0);
			markm(j,x,y);
			xx += x;
			yy += y;
			xx /=2;
			yy /=2;
			LineTo(hdc,x,y);
			swprintf(buf,L"%2d",a[i][j]);
			TextOutW(hdc,xx,yy,buf,2);
		}

	
	for(i=0;i<n;i++)
	{
		markm(i,x,y);
		swprintf(buf,L"%2c",'A'+i);
		TextOutW(hdc,x,y,buf,2);
	}
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	HWND hwndButton1,hwndButton2;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_KEYDOWN:
	{
		switch(wParam)
		{
		case 'R':   InvalidateRect(hWnd,0,1);
					break;
		case 'Q':	PostQuitMessage(0);
					break;
		}
		
		break;
	}

	case WM_CREATE:
		hwndButton1 = CreateWindowEx(0,
						TEXT("BUTTON"),
						TEXT("New Graph"),
						WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
						10,
						10,
						200,
						30,
						hWnd,
						(HMENU)IDBUTTON1,
						hInst,
						NULL);
		hwndButton2 = CreateWindowEx(0,
						TEXT("BUTTON"),
						TEXT("Step Solve"),
						WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
						220,
						10,
						200,
						30,
						hWnd,
						(HMENU)IDBUTTON2,
						hInst,
						NULL);
		break;

	case WM_COMMAND:
		
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			// char szFileName[MAX_PATH];
			ex=MessageBox(hWnd, L"Are you sure?", L"Exit",MB_ICONEXCLAMATION|  MB_OKCANCEL);
			if (ex==1)
            DestroyWindow(hWnd);
			break;
		case IDBUTTON1:
			MessageBox(hWnd,L"Optiunea nu este inca definita",L"Reminder",MB_OK);
			break;
		case IDBUTTON2:
			MessageBox(hWnd,L"Optiunea nu este inca definita",L"Reminder",MB_OK);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		
		break;
	
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
